package com.gopay.common.util.security;

import java.util.Enumeration;
import java.util.Map;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
 * 可写参数请求包装类
 * @WritableParamsRequestWrapper.java
 * @author fanghw
 * @2016年11月3日 下午3:42:47  www.gopay.com.cn Inc.All rights reserved.
 */
public class WritableParamsRequestWrapper extends HttpServletRequestWrapper {

    private Map params; 
    
    public WritableParamsRequestWrapper(HttpServletRequest request,Map newParams) {
        super(request);
        this.params=newParams;
    }
    
    @Override
    public Map getParameterMap() {
        return params;  
    }  
  
    @Override
    public Enumeration getParameterNames() {
        Vector l = new Vector(params.keySet());  
        return l.elements();  
    }  
  
    @Override
    public String[] getParameterValues(String name) {
        Object v = params.get(name);  
        if (v == null) {  
            return null;  
        } else if (v instanceof String[]) {  
            return (String[]) v;  
        } else if (v instanceof String) {  
            return new String[] { (String) v };  
        } else {  
            return new String[] { v.toString() };  
        }  
    }  
  
    @Override
    public String getParameter(String name) {
        Object v = params.get(name);  
        if (v == null) {  
            return null;  
        } else if (v instanceof String[]) {  
            String[] strArr = (String[]) v;  
            if (strArr.length > 0) {  
                return strArr[0];  
            } else {  
                return null;  
            }  
        } else if (v instanceof String) {  
            return (String) v;  
        } else {  
            return v.toString();  
        }  
    }  

}
